package com.ruoyi.car.business.service.impl;


import com.ruoyi.car.business.domain.Appointment;
import com.ruoyi.car.business.domain.Statement;
import com.ruoyi.car.business.domain.StatementItem;
import com.ruoyi.car.business.mapper.StatementItemMapper;
import com.ruoyi.car.business.query.StatementItemQuery;
import com.ruoyi.car.business.service.IAppointmentService;
import com.ruoyi.car.business.service.IStatementItemService;
import com.ruoyi.car.business.service.IStatementService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {
    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public TablePageInfo query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementItemMapper.selectForList(qo));
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        // 合理化校验
        if (items == null || items.size() <= 0) {
            throw  new GlobalException("非法操作");
        }
        // 通过删除最后一个索引的数据来获取前端传来的list集合
        // 因为最后一个数据是前端传来的优惠价格
        // 删除的同时获取到被删除的元素
        StatementItem tempItem = items.remove(items.size() - 1);
        // 获取statementID
        Long statementId = tempItem.getStatementId();
        // 获取优惠价格
        BigDecimal disCountPrice = tempItem.getItemPrice();
        // 获取 statement
        Statement statement = statementService.get(statementId);
        // 判断是否已支付
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new GlobalException("已支付不能进行保存操作");
        }
        // 删除之前的明细数据
        statementItemMapper.deleteByStatementId(statementId);
        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal totalQuantity = new BigDecimal("0.00");
        for (StatementItem item : items) {
            statementItemMapper.insert(item);
            // 计算总金额数 总消费金额 =  总消费金额+(服务项价格*购买数量) 每次循环相加一次达到累加效果
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            // 服务项数量 在每一次循环相加达到累加效果
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        // 判断折后价格是否大于优惠价格
        if (disCountPrice.compareTo(totalAmount)>0){
            throw  new GlobalException("非法操作");
        }
        // 更新结算单的总消费金额, 总数量, 总折扣数量
        statementService.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public void payStatement(Long statementId) {
        // 获取当前用户登入id
        Long userId = ShiroUtils.getUserId();
        // 修改结算单的状态
        statementService.payStatement(statementId,userId,Statement.STATUS_PAID);
        // 根据前台的支付ID查询对应的statement
        Statement statement = statementService.get(statementId);
        // 判断当前结算单是否有对应预约单, 如果有修改状态为已支付
        if (statement.getAppointmentId() != null) {
            // 根据支付ID修改为已支付状态
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_PAY);
        }
    }

    @Override
    public void deleteByStatementId(Long dictId) {
        statementItemMapper.deleteByStatementId(dictId);
    }
}
